import {ref, reactive, onMounted} from 'vue'
import { useTableData } from "../../hooks/table/useTableData.js";
import { categoryMgtAdd,categoryMgtEdit,categoryMgtDelete } from '../../api/categoryMgt.js'
import { useConfirmAction } from '../../hooks/comfirm/useConfirmAction.js'
import { ElMessage } from "element-plus";
export function useCategoryData () {
    let winTitle = ref(null)
    let type = ref(null)
    let page = ref(null)
    let currentRow = ref(null) //当前行
    let isShowWindow = ref(false)
    const { confirm } = useConfirmAction()
    const formItems = ref([
        {
            label: '分类名称',
            key: 'name',
            type: 'el-input',
            placeholder: '请输入分类名称',
            width: 400,
            rules: [
                { required: true, message: '请输入分类名称', trigger: 'blur' }
            ]
        },
        {
            label: '排序',
            key: 'sort',
            type: 'el-input',
            placeholder: '请输入排序',
            width: 400,
            rules: [
                { required: true, message: '请输入排序', trigger: 'blur' }
            ]
        },
    ])
    const formProps = reactive({
        model: {},
        formItems: formItems
    })
    let columns = ref([
        {
            label: '分类名称',
            prop: 'name',
            align: 'center'
        },
        {

            label: '分类类型',
            prop: 'type',
            align: 'center',
            isSlot: true,
        },
        {
            label: '操作时间',
            prop: 'updateTime',
            align: 'center',
            isSlot: true,
        },
        {
            label: '排序',
            prop: 'sort',
            align: 'center',
        },
        {
            label: '操作',
            prop: 'general',
            align: 'center',
            isSlot: true,
        }

    ])
    const { sizeChange, prevClick, nextClick, currentChange, getTableData, tableData, total, loading } = useTableData('/category/query')
    onMounted(() => {
        getTableData().then(r => {})
    })
    function openEditWindow (row) {
        isShowWindow.value = true
        page.value = 'edit'
        winTitle.value = '修改分类'
        for (let key in row) {
            formProps.model[key] = row[key]
        }
        currentRow.value = row
    }
    // 新增-添加
    async function cateSubMit (more) {
        let dataParams = {
            name: formProps.model['name'],
            sort: parseInt(formProps.model['sort']),
            type: type.value
        }
        if (dataParams.sort && dataParams.name) {
            if(page.value === 'add') {
                try {
                    if (more) {
                        await categoryMgtAdd(dataParams)
                        for(let key in formProps.model) {
                            formProps.model[key] = null
                        }
                    } else {
                        await categoryMgtAdd(dataParams)
                        isShowWindow.value = false
                    }
                    await getTableData()
                }catch (e) {
                    await Promise.reject(e)
                }
            } else {
                try {
                    await categoryMgtEdit({ ...dataParams, type: currentRow.value.type, id: currentRow.value.id })
                    isShowWindow.value = false
                    await getTableData()
                }catch (e) {
                    await Promise.reject(e)
                }
            }
        } else {
            ElMessage.error('请填写分类名称或序号')
        }
    }
    // 删除单个
    function deleteCateOne (row) {
        confirm('是否要删除该数据',async() => {
            await categoryMgtDelete(BigInt(row.id))
            await getTableData()
        },()=> {
            return false
        })
    }
    return {
        formProps,
        columns,
        sizeChange,
        prevClick,
        nextClick,
        currentChange,
        getTableData,
        tableData,
        winTitle,
        type,
        page,
        isShowWindow,
        openEditWindow,
        total,
        cateSubMit,
        deleteCateOne,
        loading
    }
}
